package com.site.wisdom_construction.controller.personnelModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.entity.personnelModule.Construction;
import com.site.wisdom_construction.entity.publicModule.Position;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.personnelModule.ConstructionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2022/1/4
 * @Description
 */
@Api(tags = "参建单位信息接口")
@RestController
@RequestMapping("Construction")
@CrossOrigin
public class ConstructionController {
    @Autowired
    private ConstructionService constructionService;

    @ApiOperation(value = "增加参建单位信息", notes = "测试数据:")
    @PostMapping("/insertConstruction")
    public JsonResult insertConstruction(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        List<Construction> constructionList = constructionService.findConstructionByName(map);
        if (constructionList.size() > 0) {
            jsonResult.setCode(200);
            jsonResult.setMessage("参建单位信息已存在!");
        } else {
            int i = constructionService.insertConstruction(map);
            if (i == 1) {
                jsonResult.setCode(200);
                jsonResult.setMessage("成功录入参建单位信息!");
            } else {
                jsonResult.setCode(20006);
                jsonResult.setMessage("录入参建单位信息失败!");
            }
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除参建单位信息", notes = "测试数据:")
    @PostMapping("/deleteConstruction")
    public JsonResult deleteConstruction(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = constructionService.deleteConstruction(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除参建单位信息!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除参建单位信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "修改参建单位信息", notes = "测试数据:")
    @PostMapping("/updateConstruction")
    public JsonResult updateConstruction(@RequestBody Map map) {
        JsonResult jsonResult = new JsonResult();
        int i = constructionService.updateConstruction(map);
        if (i == 1) {
            jsonResult.setCode(200);
            jsonResult.setMessage("成功修改参建单位信息!");
        } else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("修改参建单位信息失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询参建单位信息", notes = "测试数据:")
    @PostMapping("/findConstructionByPage")
    public Page findConstructionByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(constructionService.Total(map));
        page.setItems(constructionService.findConstructionByPage(map));
        return page;
    }

    @ApiOperation(value = "下拉框查询参建单位信息", notes = "测试数据:")
    @PostMapping("/selectConstruction")
    public OneDataResult selectConstruction(@RequestBody Map map) {
        OneDataResult oneDataResult = new OneDataResult();
        oneDataResult.setData(constructionService.selectConstruction(map));
        return oneDataResult;
    }
}
